import DBConnection from "../../service/unit/mysql/DBConnection";

export interface IGetArticleAllQuery {
    id:number
    title:string
}

export default function getArticleAll(conn:DBConnection,anthology:string, author:string){
    const TABLE_NAME = 'article';
    return getArticleAllQuery(conn,TABLE_NAME, anthology, author);
}

async function getArticleAllQuery(conn:DBConnection, TABLE_NAME:string, anthology:string, author:string):Promise<IGetArticleAllQuery[]>{
    const _knex = conn.knex;
    const _anthology = conn.strEncode(anthology);
    const _author = conn.strEncode(author);
    const queryResult = await _knex.where({is_active:1,author:_author,anthology:_anthology}).
    select('id','title').
    table(TABLE_NAME);
    const LENGTH = queryResult.length;

    const finalResult = [];

    for(let i=0;i<LENGTH;i++){
        const {id,title} = queryResult[i];
        const _title = conn.strDecode(title);

        finalResult[i] = {id,title:_title};
    }

    return finalResult;
}

